package com.goparry.taskman.browse;

import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import com.goparry.taskman.R;
import com.goparry.taskman.repository.RObject;
import com.goparry.taskman.repository.RObject.SortBy;
import com.goparry.taskman.restclient.AsyncRestClientRObjects;
import com.goparry.taskman.restclient.RESTUtilities;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

/**
 * @author davidp
 * Activity for managing all aspects of browsing the repository.
 * This activity hosts the BrowseFragment which the repository navigation and list UI.
 * It can also host the DocumentFragmant for showing details on a repository item.
 * The onDocumentSelected method provides the interaction between the two fragments.
 */
public class BrowseActivity extends FragmentActivity
		implements 	BrowseFragment.BrowseListeners{
	
	private String mExtraString="";
	
	@Override
	public void onCreate(Bundle icicle){ 
		super.onCreate(icicle); 
		
		// Set the layout for this tab to browse.xml
		setContentView(R.layout.browse);
	}
	@Override
	public List<RObject> onInitialSelection() {
		// This gets the initial list of objects, in this case the vaults.
		String uri = RESTUtilities.getVaultsURI();
		List<RObject> rObjects = null;
    	rObjects = getRObjects(uri);
        RObject.sortBy = SortBy.NAME;
		return rObjects;
	}
	@Override
	public void onDocumentSelected(RObject rObject) {
		//Create a Bundle to hold the rObject data and pass into the DocumentFragment
        Bundle documentBundle = new Bundle();
        documentBundle.putString("ID", rObject.getID());
        documentBundle.putString("name", rObject.getName());
        documentBundle.putString("title", rObject.getTitle());
        documentBundle.putString("revision", rObject.getRevision());
        documentBundle.putString("status", rObject.getStatus());
        documentBundle.putString("dateLastModified", rObject.getDateLastModified());
        documentBundle.putString("format", rObject.getFormat());
        documentBundle.putString("htmlProperties", rObject.getHtmlProperties());
        documentBundle.putLong("size", rObject.getSize());
        
		// Check if the DocumentFragment exists and is in the current layout.
        // If it's not then start the DocumentFragment in its own DocumentActivity.
        // If it does exist then call its updateDocument() method to display the document details.
        DocumentFragment documentFragment = (DocumentFragment) getSupportFragmentManager()
				.findFragmentById(R.id.documentfragment);
        if (documentFragment == null || !documentFragment.isInLayout()) {
            //Create an Intent to launch the document Dialogue for this task
            Intent intent = new Intent().setClass(getApplicationContext(), DocumentActivity.class);
            
            intent.putExtras(documentBundle);
            
            //Run the Activity
            startActivity(intent);       	
        } else {
        	documentFragment.updateDocument(documentBundle);
        }
	}
	@Override
	public List<RObject> onFolderSelected(String folderID) {
        // Get the documents
		List<RObject> rObjects = null;
    	String uri = RESTUtilities.getContaineesURI(folderID);
    	rObjects = getRObjects(uri);
		return rObjects;
	}
	public List<RObject> getRObjects(String uri) {
		List<RObject> rObjects = null;
		AsyncRestClientRObjects arcro = new AsyncRestClientRObjects();
		arcro.execute(uri);
		try {
			rObjects = arcro.get(20, TimeUnit.SECONDS);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TimeoutException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rObjects;
	}
	public String onGetExtraString() {
		return mExtraString;
	}
	public void setExtraString(String extraString) {
		this.mExtraString = extraString;
	}

}
